package com.greatwideweb
{
  import flash.display.DisplayObject;
  import flash.events.Event;
  import flash.display.Sprite;
  import mx.containers.ViewStack;
  import mx.effects.Move;
  import mx.events.EffectEvent;
  import mx.events.IndexChangedEvent;

  public class NavigationEffect extends Sprite
  {
    public static const IS_COMPLETE:String= "NavigationIsComplete"
    
    public function play(event:IndexChangedEvent, vs:ViewStack):void
    {
      var oldTab:DisplayObject = vs.getChildAt(event.oldIndex);
      var newTab:DisplayObject = vs.getChildAt(event.newIndex);   
      var moveEffect:Move = new Move();
      moveEffect.target = oldTab;
      moveEffect.suspendBackgroundProcessing = true;
      var moveEffect2:Move = new Move();
      moveEffect2.suspendBackgroundProcessing = true;
      moveEffect2.target = newTab;
      oldTab.visible = true;
      newTab.visible = true;
      
      if(event.oldIndex > event.newIndex)
      {
          //Moving old Tab out to the right, new tab in from the left                    
          oldTab.x = 0;
          moveEffect.xTo = oldTab.width + 100;    
          moveEffect.yFrom = oldTab.y;
          moveEffect.yTo = oldTab.y;                                
          newTab.x = -oldTab.width;
          moveEffect2.xTo = 0;        
          moveEffect2.yFrom = oldTab.y;
          moveEffect2.yTo = oldTab.y;                        
      }
      else
      {
          //Moving old Tab out to the left, new tab in from the right    
          oldTab.x = 0;
          moveEffect.xTo = -oldTab.width;            
          moveEffect.yFrom = oldTab.y;
          moveEffect.yTo = oldTab.y;                                                                        
          newTab.x = oldTab.width + 100;
          moveEffect2.xTo = 0;    
          moveEffect2.yFrom = oldTab.y;
          moveEffect2.yTo = oldTab.y;                                                                        
      }
      
      moveEffect2.play();    
      moveEffect.addEventListener(EffectEvent.EFFECT_END,tabOffScreen);
      moveEffect.play();
    }// end method
    
     public function tabOffScreen(event:EffectEvent):void
     {
      event.target.removeEventListener(EffectEvent.EFFECT_END,tabOffScreen);
      try{
        event.target.visible = false;
      }catch(e:Error){}
      //callLater(notifyChild);  
      //callBackFudisp
      dispatchEvent(new Event(IS_COMPLETE));
     }
  }
}